﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ServeNHL.Models
{
    [MetadataType(typeof(organizationMetadata))]
    public partial class ORGANIZATION
    {
        /// <summary>
        /// Given a UserRoleModel, saves or updates the information about an organization
        /// </summary>
        /// <param name="organization">Organization to add or update</param>
        /// <returns>Organization ID</returns>
        public int SaveOrganization(UserRoleModel organization)
        {
            int org_id = 0;

            if(organization.Organization.ORG_ID >= 0)
            {
                OrganizationUpdate(organization.Organization);
            }
            else
            {
                OrganizationInsert(organization.Organization);
            }

            org_id = organization.Organization.ORG_ID;

            return org_id;
        } // End of public int SaveOrganization(UserRoleModel organization)

        /// <summary>
        /// Adds a organization object to the database
        /// </summary>
        /// <param name="organization">Organization to add</param>
        /// <returns>Organization ID</returns>
        public int OrganizationInsert(ORGANIZATION organization)
        {
            int org_id = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var currentOrganization = new ORGANIZATION
                {
                    ORG_NAME = organization.ORG_NAME,
                    STATUS = organization.STATUS,
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate(),
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                };

                dbconn.ORGANIZATIONs.Add(currentOrganization);
                dbconn.SaveChanges();

                org_id = currentOrganization.ORG_ID;
            }
            return org_id;
        } // End of public int OrganizationInsert(ORGANIZATION organization)

        /// <summary>
        /// Updates an organization object in the database
        /// </summary>
        /// <param name="organization">Organization to update</param>
        /// <returns>Organization ID</returns>
        public int OrganizationUpdate(ORGANIZATION organization)
        {
            int org_id = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var tempOrganization = new ORGANIZATION();

                if (!String.IsNullOrEmpty(organization.ORG_NAME))
                {
                    tempOrganization.ORG_NAME = organization.ORG_NAME;
                }

                if (!String.IsNullOrEmpty(organization.STATUS))
                {
                    tempOrganization.STATUS = organization.STATUS;
                }

                dbconn.ORGANIZATIONs.Add(tempOrganization);
                dbconn.SaveChanges();

                org_id = tempOrganization.ORG_ID;
            }
            return org_id;
        } // End of public int OrganizationUpdate(ORGANIZATION organization)





    }
}